package com.dotools.toutiaolibrary;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.FragmentActivity;

import com.bykv.vk.openvk.TTNtExpressObject;
import com.bykv.vk.openvk.TTVfConstant;
import com.bykv.vk.openvk.TTVfDislike;
import com.bykv.vk.openvk.TTVfManager;
import com.bykv.vk.openvk.TTVfNative;
import com.bykv.vk.openvk.VfSlot;

import java.util.List;

import api.ttfeed.PreloadExpress_API_TT;

public class TT_PreloadExpress extends PreloadExpress_API_TT {

    /**
     * 个性化模板信息流广告，
     * 开发者不用自行对广告样式进行编辑和渲染，可直接调用相关接口获取广告view,广告样式模板以及微调后期支持开发者编辑
     * 2.5.xx版本后申请的KEY 后台强制要用渲染模版
     */
    private TTVfNative mTTAdNative;
    private TTNtExpressObject ttOb;


    @Override
    public void LoadTTExpress(Context context, String appid, String adposid, int expressViewWidth, int expressViewHeight, int count, int downloadMode, boolean isDebug, final TTExpressLoadListener listener) {
        if (appid != null) {
            if (appid.trim().equals("")) {
                return;
            }
        } else {
            return;
        }
        TTVfManager manager = TTManagerHolder.get();
        if (manager != null) {
            mTTAdNative = manager.createVfNative(context.getApplicationContext());
            VfSlot adSlot = new VfSlot.Builder()
                    .setCodeId(adposid)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(640, 320)
                    .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight)
                    .setAdCount(1)
                    .build();
            if (downloadMode == 1) {
                adSlot.setDownloadType(TTVfConstant.DOWNLOAD_TYPE_POPUP);
            }
            mTTAdNative.loadNtExpressVn(adSlot, new TTVfNative.NtExpressVfListener() {
                @Override
                public void onError(int i, String s) {
                    listener.onError(i, s);
                }

                @Override
                public void onNtExpressVnLoad(List<TTNtExpressObject> list) {
                    ttOb = list.get(0);
                    listener.onLoad(list.size());
                }
            });
        }else {
            listener.onError(666, "TTVfManager is null  可能原因sdk未初始化成功");
        }
    }

    @Override
    public void showExpress(final ViewGroup container,
                            final TTExpressInteractionListener listener) {
        if (ttOb != null) {
            ttOb.setExpressInteractionListener(new TTNtExpressObject.ExpressNtInteractionListener() {

                @Override
                public void onClicked(View view, int i) {
                    listener.onObClicked(i);
                }

                @Override
                public void onShow(View view, int i) {
                    listener.onObShow(i);
                }

                @Override
                public void onRenderFail(View view, String msg, int code) {
                    listener.onError(code, msg);
                }

                @Override
                public void onRenderSuccess(View view, float width, float height) {
                    listener.onRenderSuccess();
                    container.removeAllViews();
                    container.addView(view);
                }
            });
            ttOb.render();
        } else {
            listener.onError(666, "TTNativeExpressOb is null");
        }
    }

    @Override
    public void bindDislike(FragmentActivity activity, final ViewGroup container) {
        if (ttOb != null) {
            //使用默认模板中默认dislike弹出样式
            ttOb.setDislikeCallback(activity, new TTVfDislike.DislikeInteractionCallback() {

                @Override
                public void onShow() {
                }

                @Override
                public void onSelected(int i, String s, boolean b) {
                    //用户选择不喜欢原因后，移除广告展示
                    container.removeAllViews();
                }

                @Override
                public void onCancel() {
                }

            });
        }

    }

    @Override
    public void expressDestroy() {
        if (ttOb != null) {
            ttOb.destroy();
        }
    }
}
